package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.mapper.MemberReservationMapper;
import com.zzyl.nursing.service.IMemberReservationService;
import com.zzyl.nursing.vo.TimeCountVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.MethodWrapper;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class MemberReservationServiceImpl extends ServiceImpl<MemberReservationMapper, Reservation> implements IMemberReservationService {
    @Autowired
    private MemberReservationMapper memberReservationMapper;

    @Override
    public int getCancelledReservationCount(Long userId) {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);

        LambdaQueryWrapper<Reservation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Reservation::getUpdateBy,userId)
                .between(Reservation::getUpdateTime,startTime,endTime);

        return (int) count(lambdaQueryWrapper);

    }

    /**
     * 查询每个时间段剩余预约次数
     * @return
     */
    @Override
    public List<TimeCountVo> countReservationsForTime(Long time) {

        //2025-06-06
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
        //2025-06-06 00:00:00
        LocalDateTime startTime = localDateTime.toLocalDate().atStartOfDay();
        //2025-06-06 00:00:00
        LocalDateTime endTime = startTime.plusHours(24);
        List<TimeCountVo> timeCountVoList = memberReservationMapper.countReservationsForTime(startTime,endTime);
        return timeCountVoList;
    }

    /**
     * 新增预约信息
     */
    @Override
    public void addReservations(ReservationDto reservationDto) {
        Long userId = UserThreadLocal.getUserId();
        Reservation reservation = BeanUtil.toBean(reservationDto, Reservation.class);
        reservation.setCreateBy(userId.toString());
        reservation.setUpdateBy(userId.toString());
        reservation.setStatus(0);
        save(reservation);
    }

    /**
     * 分页查询预约信息
     */
    @Override
    public List<Reservation> pageQuery(Integer status) {
        Long userId = UserThreadLocal.getUserId();
        if (ObjectUtil.isEmpty(userId)){
            throw new BaseException("用户未登录");
        }
        List<Reservation> list = lambdaQuery().eq(Reservation::getUpdateBy, userId)
                .eq(ObjectUtil.isNotEmpty(status), Reservation::getStatus, status)
                .orderByDesc(Reservation::getUpdateTime)
                .list();
        return list;
    }

    /**
     * 取消预约
     * @param id
     */
    @Override
    public void cancelReservation(Long id) {
        Reservation reservation = getById(id);
        if (ObjectUtil.isEmpty(reservation)){
            throw new BaseException("预约不存在");
        }
        reservation.setStatus(2);
        reservation.setUpdateBy(UserThreadLocal.getUserId().toString());
        updateById(reservation);
    }
}
